//
//  ZYCurveSelectView.m
//  YiLin
//
//  Created by Zyark on 2018/6/6.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYCurveSelectView.h"
#import "ZYCurve.h"
#import "UIButton+ImageTitleSpacing.h"

#define LEFT 30
#define MARGIN 5
#define TOP 10
@interface ZYCurveSelectView()
@property(nonatomic,strong) NSMutableArray *buttonArray;
@property(nonatomic,strong) NSArray *curveArray;
@end

@implementation ZYCurveSelectView

-(instancetype)initWithFrame:(CGRect)frame curveArray:(NSArray *)curveArray{
    self = [super initWithFrame:frame];
    if (self) {
        _curveArray = curveArray;
        [self setup];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSystemCurve];
    }
    return self;
}

-(void)setupSystemCurve{
    NSArray<NSNumber *> *modeTypeArray = @[[NSNumber numberWithUnsignedInteger:ZYCurveTypeAutomaticLearning],
                                           [NSNumber numberWithUnsignedInteger:ZYCurveTypeElderlyChildren],
                                           [NSNumber numberWithUnsignedInteger:ZYCurveTypeSouth],
                                           [NSNumber numberWithUnsignedInteger:ZYCurveTypeNorth]];
    NSArray<NSString *> *titleArray = @[LC_CURVE_TYPE_AUTOMATIC_LEARNING,
                                        LC_CURVE_TYPE_ELDERLY_CHILDREN,
                                        LC_CURVE_TYPE_SOUTH,
                                        LC_CURVE_TYPE_NORTH];
    NSMutableArray *array = [NSMutableArray array];
    for (NSUInteger i=0; i<4; i++) {
        ZYCurve *model = [[ZYCurve alloc] init];
        model.type = [[modeTypeArray objectAtIndex:i] unsignedIntegerValue];
        model.name = [titleArray objectAtIndex:i];
        [array addObject:model];
    }
    _curveArray = array;
    [self setup];
}

-(void)setup{
    int index = 0;
    _buttonArray = [NSMutableArray arrayWithCapacity:[_curveArray count]];
    for (ZYCurve *curve in _curveArray) {
        int column = index %  2;//列
        int row = floor(index / 2);//行
        UIButton *button = [self setupButton:curve];
        button.left = column *(button.width+MARGIN) + LEFT;
        button.top = TOP + row * (TOP + button.height);
        index ++;
        [_buttonArray addObject:button];
    }
    UIButton *lastButton = [_buttonArray lastObject];
    self.height = lastButton.bottom + TOP;
}

-(UIButton *)setupButton:(ZYCurve *)curve{
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    UIButton *button = [[UIButton alloc] init];
    button.size = CGSizeMake(self.width /2 - LEFT - MARGIN, 35);
    
    [button setTitle:[language getStringByKey:curve.name] forState:UIControlStateNormal];
    [button setTitle:[language getStringByKey:curve.name] forState:UIControlStateSelected];
    [button setTitleColor:RGBA(71, 72, 74, 1) forState:UIControlStateNormal];
    [button setTitleColor:RGBA(71, 72, 74, 1) forState:UIControlStateSelected];
    [button setImage:[UIImage imageNamed:@"ic_quick_setup_curve_btn_unselected"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"ic_quick_setup_curve_btn_selected"] forState:UIControlStateSelected];
    [button.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [button addTarget:self action:@selector(doSelect:) forControlEvents:UIControlEventTouchUpInside];
    [button layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:10];
    /*    top: 为正数：表示向下偏移  为负数：表示向上偏移
    *   left: 为整数：表示向右偏移  为负数：表示向左偏移
    * bottom: 为整数：表示向上偏移  为负数：表示向下偏移
    *  right: 为整数：表示向左偏移  为负数：表示向右偏移
    *
    **/
    CGFloat left = -(button.width - button.titleLabel.width)+ button.imageView.frame.size.width;
    button.imageEdgeInsets = UIEdgeInsetsMake(0, left, 0, 0);//
    button.titleEdgeInsets = UIEdgeInsetsMake(0, left + 15, 0, 0);
    button.tag = curve.type;
    [self addSubview:button];
    return button;
}

-(void)setIsCheck:(NSArray *)array{
    NSUInteger i = 0;
    for (NSNumber *number in array) {
        UIButton *button = [_buttonArray objectAtIndex:i];
        [button setSelected:[number boolValue]];
        i++;
    }
}

-(void)doSelect:(UIButton *)button{
    button.selected = !button.isSelected;
}

@end
